<?php
/**
 * Created by PhpStorm.
 * User: clcw908
 * Date: 2016/12/23
 * Time: 9:31
 */

namespace  AuctionBack\Controller;

use AuctionBack\Logic\RefundLogic;
use AuctionBack\Logic\RefundCarTraceLogLogic;
use AuctionBack\Logic\CarReportLogic;
use AuctionCore\Account\Back\Employee;
use AuctionCore\Company\CompanyManage;

class RefundCarController extends BaseController {

	/**
	 * @var
	 */
	protected $em_id;

	/**
	 * 构造函数
	 *
	 * RefundCarController constructor.
	 */
	public function __construct()
	{
		parent::__construct();
		$this->em_id = $this->user_info['emp_id'];
		$bc_list = $this->get_branch_company_list();
		$this->assign('bc_list', $bc_list);
	}

	/**
	 * 列表页展示
	 */
	public function index(){
		$this->display();
	}

	/**
	 * 得到列表数据
	 */
	public function getList(){
		$params = [];
		$page = I("get.p/d", 1);
		$size = 10;
		$status = I('get.t',1);
		$type = I('get.type');
		$bc_city = I('get.bc_city');

		if($status == 3){ //如果是已退车列表
			$startTime = I('get.start_time');
			$endTime = I('get.end_time');
			$params['start_time'] = $startTime;
			$params['end_time'] = $endTime;
		}
		//检查权限
		$rules = "TuiCheGuanLiChaKanSuoYou";
		$e = new Employee($this->user_info['username']);
		$checkResult  = $e->check_auth($rules);
		if(!$checkResult){
			$params['finish_emp_id'] = $this->em_id;
		}

		$location_area = [];
		if($bc_city){
			$cityInfo = CompanyManage::getCityCodeByBranchId($bc_city);
			foreach ($cityInfo as $key => $value){
				$location_area[] = $value['city_code'];
			}
		}else{
			$location_area = $this->user_info['bc_code'];
		}
		$params['location_area'] = $location_area;

		$params['status'] = $status;
		$params['type'] = $type;
		$params['car_plate_number'] = trim(I("get.car_plate_number", ''));
		$params['sno'] = trim(I("get.sno", ''));


		$result = RefundLogic::getList($page,$size,$params);
		$Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
		$Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
		$show = $Page->show(); // 分页显示输出

		//模板赋值
		$this->assign('status',$status);
		$this->assign('page', $show); // 赋值分页输出
		$this->assign('list', $result['list']);
		$this->assign("params", $params);
		$this->display('RefundCar/refund_car_list');
	}

	/**
	 * 得到退车详情
	 */
	public function refund_car_info(){
		$id = I('get.id');
		$status = I('get.status');
		$refundCarObj = new RefundLogic($id);
		$detail = $refundCarObj->detail();
		$detail['car_source'] = 1;

		$followLog = RefundCarTraceLogLogic::getDataByRefundCarId($id);
		/*echo "<pre>";
		print_r($detail);
		echo "</pre>";
		die();*/
		$this->assign('status',$status);
		$this->assign('follow_log',$followLog);
		$this->assign('detail',$detail);
		$this->display();
	}

	/**
	 * 完成退车
	 */
	public function save(){
		$id = I('post.id');
		$carId = I('post.car_id'); //车辆编号
		$ownerId = I('post.owner_id');
		$orderId = I('post.order_id'); //拍单号
		if($id < 0){
			$this->helpJsonResult(422,'退车信息不存在');
		}
		$procedures = I('post.procedures'); //核实手续信息
		if(empty($procedures)){
			$this->helpJsonResult(422,'手续不能为空');
		}
		$driverCompany = I('post.driver_company'); //代驾公司
		if(empty($driverCompany)){
			$this->helpJsonResult(422,'代驾公司不能为空');
		}
		$driverName = I('post.driver_name'); //代驾姓名
		if(empty($driverName)){
			$this->helpJsonResult(422,'代驾姓名不能为空');
		}
		$driverPhone = I('post.driver_phone'); //代驾电话
		if(empty($driverPhone)){
			$this->helpJsonResult(422,'代驾电话不能为空');
		}
		$departureTime = I('post.departure_time'); //代驾出发时间
		$remark = I('post.remark'); //退车备注

		M()->startTrans();
		$data = [
			'procedures' => $procedures,
			'driver_company' => $driverCompany,
			'driver_name' => $driverName,
			'driver_phone' => $driverPhone,
			'departure_time' => $departureTime,
			'finish_emp_id' => $this->user_info['emp_id'],
			'finish_emp_name' => $this->user_info['real_name'],
			'finish_time' => date('Y-m-d H:i:s',time()),
			'update_time' => date('Y-m-d H:i:s',time()),
			'remark' => $remark,
			'status' => 3,
		];
		$refundObj = new RefundLogic($id);

		$result = $refundObj->save($data);
		$code = 200;
		$msg = '退车成功';
		if(!$result){
			M()->rollback();
			$code = 422;
			$msg = '更新退车信息失败';
			$this->helpJsonResult($code,$msg);
		}


		$carReport = new CarReportLogic($carId);
		$result = $carReport->saveByCarId(['car_status'=>11]); //已退车
		if(!$result){
			M()->rollback();
			$code = 422;
			$msg = '更新车辆报告信息失败';
			$this->helpJsonResult($code,$msg);
		}
		M()->commit();
		$this->record_log($orderId,$carId,$ownerId,$msg);
		$this->helpJsonResult($code,$msg);
	}


	/**
	 * 继续跟踪
	 */
	public function continueFollow(){
		$id = I('post.id');
		$carId = I('post.car_id');
		$nextTraceTime = I('post.next_trace_time'); // 下次跟踪时间
		$ownerId = I('post.owner_id');
		$orderId = I('post.order_id'); //拍单号
		$traceReason = I('post.trace_reason'); //继续跟踪原因
		$procedures = I('post.procedures'); //手续信息
		$data = [
			'update_time' => date('Y-m-d H:i:s',time()),
			'next_trace_time' => $nextTraceTime,
			'procedures' => $procedures,
			'finish_emp_id' => $this->user_info['emp_id'], //继续跟踪人
			'finish_emp_name' => $this->user_info['real_name'], //继续跟踪人姓名
			'status' => 2,
		];

		M()->startTrans();

		$refundObj = new RefundLogic($id);
		$result = $refundObj->save($data);

		$code = 200;
		$msg = '继续跟踪成功';
		if(!$result){
			M()->rollback();
			$code = 422;
			$msg = '更新退车信息失败';
			$this->helpJsonResult($code,$msg);
		}

		$traceData = [
			'rc_id' => $id,
			'order_id' => $orderId,
			'emp_id' => $this->user_info['emp_id'],
			'emp_name' => $this->user_info['real_name'],
			'action' => '继续跟踪',
			'remark' => $traceReason,
			'create_time' => date('Y-m-d H:i:s',time()),
		];
		$result = RefundCarTraceLogLogic::add($traceData);
		if(!$result){
			M()->rollback();
			$code = 422;
			$msg = '继续跟踪失败';
			$this->helpJsonResult($code,$msg);
		}
		M()->commit();

		$this->record_log($orderId,$carId,$ownerId,$msg);
		$this->helpJsonResult($code,$msg);
	}

	/**
	 * 记录操作日志
	 *
	 * @param $order_id 拍单号
	 * @param $car_id 报告编号
	 * @param $owner_id 车主id
	 * @param $action 行为
	 */
	private function record_log($order_id, $car_id, $owner_id, $action)
	{
		//记录车辆跟踪日志
		\AuctionCore\CarResource\Car::add_car_trace_log($car_id, [
			'action_name' => $action,
			'emp_id' => $this->emp_id
		]);
		//记录拍品跟踪日志
		\AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($order_id, [
			'action_name' => $action,
			'emp_id' => $this->emp_id
		]);
		//记录员工操作日志
		\AuctionCore\System\SystemLog::add([
			'emp_id' => $this->emp_id,
			'order_id' => $order_id,
			'car_id' => $car_id,
			'owner_id' => $owner_id,
			'action' => $action
		]);
	}

}